from flask import Flask, render_template, url_for, request, flash, session, redirect
from flask_sqlalchemy import SQLAlchemy
1

# https://www.youtube.com/watch?v=XUwOOqRDAHE
# https://github.com/deeaarbee/Flask-Login-Module 

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///newflask.db'
db = SQLAlchemy(app)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(300), nullable=False)
    text = db.Column(db.Text, nullable=False)

# Определяем маршрут для отображения "Hello, World!"
@app.route('/')
def index():
    print(url_for('index'))
    return render_template('index.html', title='О сайте')

@app.errorhandler(404)
def pageNotFound(error):
    return render_template('page404.html', title="Page not found"), 404

@app.route('/create', methods=['POST, GET'])
def create():
    if request.method == 'POST':
        # request.form['title']
        print(request.form['title'])
    else:
        return render_template('create.html')


@app.route('/about')
def about():
    return render_template('about.html')


@app.route('/login', methods=['POST', 'GET'])
def login():
    if 'userLogged' in session:
        return redirect(url_for('profile', username=session['userLogged']))
    elif request.method == 'POST' and request.form['username'] == 'selfedu' and request.form['psw'] == '123':
        session['userLogged'] = request.form['username']
        return redirect(url_for('profile', username=session['userLogged']))
    return render_template('login.html', title='Authorization')


@app.route('/profile/<username>')
def profile(username):
    return username


# with app.test_request_context():
#     print(url_for('index'))



# Запускаем веб-сервер Flask
if __name__ == '__main__':
    app.run(debug=True)